/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 11-4-29
 * Time: 下午5:29
 * To change this template use File | Settings | File Templates.
 */ 


var Search	=	Class.create();
Object.extend(Search, {
	filtreKey:'',
	checkFrom:function(f){
		var form	=	$(f);
		var v = $('keywords').value.trim();
		if(v.length<=0 || v == '请输入漫画名或者作者'){
			new MessageBox(MessageBox.TYPE_MESSAGE, '请输入漫画名或者作者名', '错误', {});
			return false;
		}else{
			form.submit();
		}
	},
	_onfocus:function(){
		if($('keywords').value.trim() == '请输入漫画名或者作者')	{
			$('keywords').value = '';
		}
	},
	_onblur:function(){
		if($('keywords').value.trim() == ''){
			$('keywords').value = '请输入漫画名或者作者'
		}
	}
});

var Log = Class.create();
Object.extend(Log, {
    /**
     * 想读
     */
    read_flag:1,
    read:function(cartoon_id,type){
        if(!cartoon_id || !type) return false;
        var url = '/~index.log.log';
        this.read_flag = type;
        new Ajax.Request(url,{
			method: 'post',
			parameters: $H({cid:cartoon_id,type:type}),
			onComplete: this.complete.bindAsEventListener(this)
		});
    },
    complete:function(transport){
    	var ret = transport.responseText.evalJSON();
    	var el = 'wantread';
    	var __msg__	=	'“想读”';
    	if(this.read_flag ==2){
    		el = "reanded";
    		__msg__ = '“读过”';
    	}
    	var el = this.read_flag==1?'wantread':'reanded'
    	if(ret.result){
    		if(ret.data == -1)
    			new MessageBox(MessageBox.TYPE_MESSAGE,'您已经设置过'+__msg__+'了',"提示");
    		else{
    			var num = parseInt($(el).innerHTML)+1;
    			$(el).innerHTML = num;
    		}
    	}else{
    		if(ret.reason == -100){
    			alert('您还没有登录，不能进行本次操作，请先登录！')
    			_popup_login();
    			//new MessageBox(MessageBox.TYPE_YES_NO,'您没有登录，不能进行此操作。请先登录',"提示",{yes:function(){_popup_login();}});
    		}
    	}
    	return ;
    }
});
var Share = Class.create();
Object.extend(Share, {
    /**
     * 想读
     */
    add_share:function(cid){
        if(!cid) return false;
        var url = '/~index.cartoon.share';
        new Ajax.Request(url,{
			method: 'post',
			parameters: $H({cid:cid}),
			onComplete: this.complete.bindAsEventListener(this)
		});
    },
    complete:function(transport){
    	var ret = transport.responseText.evalJSON();
    	var __ret__ = ret.reason;
    	if(ret.result){
    		new MessageBox(MessageBox.TYPE_MESSAGE,'分享成功！',"提示");
    		return true;
    	}else{
    		if(__ret__ == -100){
    			alert('您还没有登录，不能进行本次操作，请先登录！')
    			_popup_login();
    		}
    		else if(__ret__ == -112){
    			new MessageBox(MessageBox.TYPE_MESSAGE,'您已经分享过了',"警告");
    			return false;
    		}else{
    			new MessageBox(MessageBox.TYPE_MESSAGE,'您分享失败，请稍候再试',"提示");
    			return false;
    		}
    	}
    	return ;
    }
});
/**
 * 打分
 */
var Score = Class.create();
Object.extend(Score,{
	gradeRes:function(score){
		if(!score){
			new MessageBox(MessageBox.TYPE_MESSAGE,'请选择您的评分', '提示',{}, {width:400});
			return false;
		}
		var resid = $('cartoon_id').value;

		var url = "/~index.index.score";
		new Ajax.Request(url,{
			parameters: $H({resid:resid,score:score}),
			onComplete: function(transport){
				var ret = transport.responseText.evalJSON();
				if(ret.result){
					new MessageBox(MessageBox.TYPE_MESSAGE,'谢谢您的评分!', '提示',{}, {width:400});
					location.reload();
				}else if(ret.reason == -112){
					new MessageBox(MessageBox.TYPE_MESSAGE,'您已经评过分了', '提示',{}, {width:400});
					return false;
				}
				else{
					new MessageBox(MessageBox.TYPE_MESSAGE,'评分失败，请稍后再试', '提示',{}, {width:400});
					return false;
				}
			}
		});
	}
}),
function key_press(){
	var event = event || window.event;
	if(event.keyCode==13){
		Search.checkFrom('searchForm');
	}
}